﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;

using System.Drawing;
using System.Windows.Forms;

using Athena.Objects;
using Athena.Core;
using Athena.Plugins;

namespace Athena.Tools
{
    public class DefaultsTool : INonVisualTool
    {
        public override bool Handles(List<Type> oTypes)
        {
            bool bResult = true;
            foreach (Type t in oTypes)
            {
                bResult &= Handles(t);
                if (!bResult) return false;
            }
            return bResult;
        }

        private bool Handles(Type t)
        {
            return Utility.IsTypeNoAbstract(typeof(Athena.Objects.IDrawable), t);
        }
        class ToolCommand
        {
            private string _sCommand;

            public string Command
            {
                get { return _sCommand; }
                set { _sCommand = value; }
            }

            private Bitmap _Icon;
            public Bitmap Icon
            {
                get
                {
                    return _Icon;
                }
                set
                {
                    _Icon = value;
                }
            }

            private string _Description;
            public string Description
            {
                get
                {
                    return _Description;
                }
                set
                {
                    _Description = value;
                }
            }


            public ToolCommand()
            {
                _sCommand = "-";
                _Icon = null;
            }

            /// <summary>
            /// Initializes a new instance of the ToolCommand class.
            /// </summary>
            /// <param name="sCommand"></param>
            /// <param name="icon"></param>
            /// <param name="description"></param>
            public ToolCommand(string sCommand, Bitmap icon, string description)
            {
                _sCommand = sCommand;
                _Icon = icon;
                _Description = description;
            }

            public ToolStripItem ToMenuItem()
            {
                if (_Icon == null && _sCommand == null)
                {
                    return new ToolStripSeparator();
                }
                else
                {
                    ToolStripMenuItem oItem = new ToolStripMenuItem(_sCommand, _Icon);
                    oItem.Tag = _sCommand;
                    oItem.ToolTipText = _Description;
                    oItem.ImageScaling = ToolStripItemImageScaling.None;
                    oItem.Click += new EventHandler(CommandExecute);
                    return oItem;
                }
            }

            public ToolStripItem ToToolStripButton()
            {
                if (_Icon == null)
                {
                    return new ToolStripSeparator();
                }
                else
                {
                    ToolStripButton oButton = new ToolStripButton(_sCommand, _Icon);
                    oButton.DisplayStyle = ToolStripItemDisplayStyle.Image;
                    oButton.Tag = _sCommand;
                    oButton.ImageScaling = ToolStripItemImageScaling.None;
                    oButton.ToolTipText = _Description;
                    oButton.Click += new EventHandler(CommandExecute);
                    return oButton;
                }
            }

            void CommandExecute(object sender, EventArgs e)
            {
                string sTag = "";
                if (sender is ToolStripButton)
                    sTag = (string)((ToolStripButton)sender).Tag;
                else if (sender is ToolStripMenuItem)
                    sTag = (string)((ToolStripMenuItem)sender).Tag;
                else
                    return;
                switch (sTag)
                {
                    case "Set as Default":
                        if (CurrentModel.Selected.Count > 0)
                        {
                            foreach (IDrawable id in CurrentModel.Selected)
                            {
                                if (id is ModuleConnector)
                                {
                                    ModuleConnector.DEFAULT = id as ModuleConnector;
                                }
                                else
                                if (id is BezierConnector)
                                {
                                    BezierConnector.DEFAULT = id as BezierConnector;                                    
                                }
                                else
                                if (id is SpeciesGlyph)
                                {
                                    SpeciesGlyph.DEFAULT = id as SpeciesGlyph;
                                }
                                else
                                if (id is PartGlyph)
                                {
                                    PartGlyph.DEFAULT = id as PartGlyph;
                                }
                                else
                                if (id is ContainerGlyph)
                                {
                                    ContainerGlyph.DEFAULT = id as ContainerGlyph;
                                }
                                string filename = Utility.CurrentDirectory + "\\Defaults\\" + id.GetType().ToString() + ".xml";
                                XmlTextWriter writer = new XmlTextWriter(filename,null);
                                IO.XMLWriter.WriteDrawable(id, null, writer);
                                writer.Close();
                            }
                        }
                        break;
                    default:
                        break;
                }
            }
        }
       
        static ToolCommand[] _oCommands = new ToolCommand[]
        {
            new ToolCommand("Set as Default",  new Bitmap(20,20), "Set the current appearance features as the default for this item")
        };

        private static DefaultsTool _oInstance = new DefaultsTool();
        public static DefaultsTool Instance
        {
            get { return _oInstance; }
        }

        private DefaultsTool()
        {
            _bHasMenuItems = false;
            // has items to put into context menu
            _bHasContextMenuItems = true;
            _bRequiresNewToolWindow = false;
            ToolManager.Instance.RegisterTool("Default Appearance", "Sets default appreance for selected items", this);
            try
            {
                string[] files = Directory.GetFiles(Utility.CurrentDirectory + "\\Defaults\\");

                foreach (string file in files)
                {
                    XmlTextReader reader = new XmlTextReader(file);
                    if (reader.IsEmptyElement || reader.NodeType == XmlNodeType.None) continue;
                    IDrawable id = IO.XMLReader.ReadDrawable(reader, null, null);
                    reader.Close();

                    if (id is ModuleConnector)
                    {
                        ModuleConnector.DEFAULT = id as ModuleConnector;
                    }
                    else
                        if (id is BezierConnector)
                        {
                            BezierConnector.DEFAULT = id as BezierConnector;
                        }
                        else
                            if (id is SpeciesGlyph)
                            {
                                SpeciesGlyph.DEFAULT = id as SpeciesGlyph;
                            }
                            else
                                if (id is PartGlyph)
                                {
                                    PartGlyph.DEFAULT = id as PartGlyph;
                                }
                                else
                                    if (id is ContainerGlyph)
                                    {
                                        ContainerGlyph.DEFAULT = id as ContainerGlyph;
                                    }
                }
            }
            catch (Exception)
            {
                Utility.SetMessage("default appearances were not loaded...");
            }
        }

        public override ToolManager.Order Order
        {
            get
            {
                return ToolManager.Order.FRONT;
            }
        }

        public override void SetupToolStrip(ToolStrip oStrip)
        {
        }
        public override void SetupContextMenu(ToolStripMenuItem oStrip)
        {
            foreach (ToolCommand command in _oCommands)
            {
                oStrip.DropDownItems.Add(command.ToMenuItem());
            }
        }

    }
}
